/**
 * Created by Administrator on 2016/10/16.
 */
angular.module('user.controller', [])
    .controller('userRegisterController', ['$userService', '$stateParams', '$scope','$state',
        function ($userService, $stateParams, $scope, $state) {
            $scope.user = {};
            /*
             user.email
             user.phoneNumber
             user.password
             user.confirmPassword
             */
            $scope.checkResult = {
                isValid: false,
                validMessage: '',
                validType:''
            }
            $scope.registerSuccess = false;

            $scope.register = function () {
                $scope.checkRegisterInfo();

                if ($scope.checkResult.isValid) {
                    //TODO
                    $userService.registerUser({email:$scope.user.email,phoneNumber:$scope.user.phoneNumber,password:$scope.user.password},function(data){
                        if(data && data.data){
                            var resultData = data.data;
                            console.log('result DATA :'+resultData);

                            if(resultData.result){
                                $scope.registerSuccess = true;
                                setTimeout(function(){
                                    $state.go('login');
                                },1000);
                            }
                            else {
                                $scope.checkResult.isValid = false;
                                $scope.checkResult.validMessage=resultData.message;
                                $scope.checkResult.validType='submit';
                            }

                        }else {
                            $scope.checkResult.isValid = false;
                            $scope.checkResult.validMessage="注册失败,请求超时";
                            $scope.checkResult.validType='submit';
                        }
                    },function(error){
                        $scope.checkResult.isValid = false;
                        $scope.checkResult.validMessage="注册失败";
                        $scope.checkResult.validType='submit';
                    })

                }
            }

            $scope.checkRegisterInfo = function () {
                $scope.checkResult = {
                    isValid: false,
                    validMessage: '',
                    validType:''
                }

                var isValid = verifyEmail($scope.user.email)
                if (!isValid) {
                    $scope.checkResult.isValid = isValid;
                    $scope.checkResult.validMessage = "邮箱格式不正确";
                    $scope.checkResult.validType = 'email';
                    return;
                }

                if (!$scope.user.password) {
                    $scope.checkResult.isValid = false;
                    $scope.checkResult.validMessage = "请输入密码";
                    $scope.checkResult.validType = 'password';
                    return;
                }

                if ($scope.user.password.length  < 6) {
                    $scope.checkResult.isValid = false;
                    $scope.checkResult.validMessage = "密码长度小于6";
                    $scope.checkResult.validType = 'password';
                    return;
                }

                if ($scope.user.password != $scope.user.confirmPassword) {
                    $scope.checkResult.isValid = false;
                    $scope.checkResult.validMessage = "密码输入不一致";
                    $scope.checkResult.validType = 'confirmPassword';
                    return;
                }

                var isValid = verifyPhoneNum($scope.user.phoneNumber);
                if (!isValid) {
                    $scope.checkResult.isValid = isValid;
                    $scope.checkResult.validMessage = "手机格式不正确";
                    $scope.checkResult.validType = 'phoneNumber';
                    return;
                }

                $scope.checkResult.isValid = true;

            }

        }])
